package com.hk.Mgr.web.controller.app.yingmi;

/**
 * Created by lipei on 2017/9/25.
 */

import com.hk.app.utils.YingmiOpenAPI;
import com.hk.commons.StringHelper;
import com.hk.commons.utils.sys.MapHelper;
import com.hk.yingmi.YingmiOpenAPIConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;

/**
 * Created by lipei on 2017/9/25.
 */
@Component
public class FundOrderHelper implements Callable<List<Map>> {

    public FundOrderHelper() {
    }

    @Autowired
    private YingmiOpenAPI openAPI;


    private static Logger logger = LoggerFactory.getLogger(FundOrderHelper.class);

    private static final long serialVersionUID = 5668741830426583548L;
    private String brokerUserId;
    private String accountId;
    private int curPage;
    private String fundOrderCode;
    private volatile  List pages = new ArrayList<>();

    private volatile  int maxPage = 1000;

    public void setPages(){
        for(int i = curPage; i < curPage + 5; i++){
            pages.add(String.valueOf(i));
        }
    }

    public String getPage() {
        logger.info("pages = " + pages.toString());
        String  page = pages.get(0).toString();
        pages.remove(0);
        logger.info("get page  = " + page);
        return  page;
    }



    public String getAccountId() {
        return accountId;
    }

    public void setAccountId(String accountId) {
        this.accountId = accountId;
    }

    public String getBrokerUserId() {
        return brokerUserId;
    }

    public void setBrokerUserId(String brokerUserId) {
        this.brokerUserId = brokerUserId;
    }

    public int getCurPage() {
        return curPage;
    }

    public void setCurPage(int curPage) {
        this.curPage = curPage;
        this.setPages();
    }


    public void setFundOrderCode(String fundOrderCode) {
        this.fundOrderCode = fundOrderCode;
    }

    public List<Map> getTotalOrders(){
        String page = this.getPage();
        Integer pageInt = Integer.valueOf(page);

        if(pageInt > maxPage){
            return null;
        }
        logger.info("in the getTotalOrders method");


        Map<String, String> parMap = new HashMap<>();
        parMap.put("brokerUserId",  this.brokerUserId);
        parMap.put("accountId",  this.accountId);
        parMap.put("size", "50");
        parMap.put("fundOrderCode", this.fundOrderCode);
        parMap.put("page", page);
        logger.info("getOrderListByOrderCode parm Map = {}", parMap);
        String resultStr = openAPI.sslGet_ByMap(YingmiOpenAPIConstant.FUND_ORDERS, parMap, null);

        logger.info(" page = " + page + " resultStr = {}", resultStr);
        List<Map> orderList = openAPI.getArrayList(resultStr).parallelStream()
                .filter(n->n.get("confirmStatus").toString().equals("2"))
                .collect(Collectors.toList());


        if((orderList == null  || orderList.size() == 0 )&& pageInt <  maxPage){
            maxPage = pageInt;
        }

        return  orderList;

    }


    @Override
    public List<Map> call() throws Exception {
        logger.info("in the call method");
        List<Map> orderList = this.getTotalOrders();

        return orderList;
    }



}
